package com.example.myapplication9.week4;


import com.example.myapplication9.week4.jsjf.LinearNode;
import com.example.myapplication9.week4.jsjf.exceptions.ElementNotFoundException;
import com.example.myapplication9.week4.jsjf.exceptions.EmptyCollectionException;
import com.example.myapplication9.week4.jsjf.exceptions.NonComparableElementException;

public class ProtectedlistofCourse {
     protected int count;
    protected LinearNode<Course> head, tail;
    public ProtectedlistofCourse()
    {
        count = 0;
        head = tail = null;

    }
    public Course removeFirst() throws EmptyCollectionException
    {
        if(isEmpty())
            throw new EmptyCollectionException("LinkedList");

        LinearNode <Course> list=head;
        if(size()==1)
        {
            head=tail=null;

        }
        else {
            head=head.getNext();
        }
        return  list.getElement();
    }
    public int size()
    {
        return count;
    }
    public boolean isEmpty()
    {
        if(count==0)
            return true;
        else
            return false;
    }
    public Course removeLast() throws EmptyCollectionException
    {
        if(isEmpty())
            throw new EmptyCollectionException("LinkedList");

        LinearNode<Course> list=tail;
        if(size()==1)
            head=tail=null;
        else
        {
            LinearNode list1=head;
            while(list1.getNext().getNext()!=null)
            {
                list1=list1.getNext();
            }
            tail=list1;
            tail.setNext(null);

        }
        count--;
        return list.getElement();
    }
    public Course remove(Course targetElement) throws EmptyCollectionException,
            ElementNotFoundException
    {
        if (isEmpty())
            throw new EmptyCollectionException("LinkedList");

        boolean found = false;
        LinearNode<Course> previous = null;
        LinearNode<Course> current = head;

        while (current != null && !found)
            if (targetElement.equals(current.getElement()))
                found = true;
            else {
                previous = current;
                current = current.getNext();
            }

        if (!found)
            throw new ElementNotFoundException("LinkedList");

        if (size() == 1)  // 列表中只有一个元素
            head = tail = null;
        else if (current.equals(head))  // 目标在头部
            head = current.getNext();
        else if (current.equals(tail))  // 目标在尾部
        {
            tail = previous;
            tail.setNext(null);
        }
        else  // 目标在中间
            previous.setNext(current.getNext());

        count--;


        return current.getElement();
    }
    public Course first() throws EmptyCollectionException
    {
        if(isEmpty())
            throw new EmptyCollectionException("LinkedList");

        return head.getElement();
    }



    public Course last() throws EmptyCollectionException
    {
        if(isEmpty())
            throw new EmptyCollectionException("LinkedList");

        return tail.getElement();
    }


    public boolean contains(Course targetElement) throws
            EmptyCollectionException
    {
        LinearNode current=head;
        while(current.getElement()!=targetElement&&current.getNext().getElement()!=null)
        {
            current=current.getNext();
        }
        if(current.getElement()==targetElement)
            return true;
        else
            return false;
    }
    public void add(Course element) {
        if (!(element instanceof Comparable))
            throw new NonComparableElementException("OrderedList");
        LinearNode<Course> list = new LinearNode<Course>(element);
        if(count==0) {
            head = tail = list;
            count++;
        }
        else{
            if (count==1)
            {
                if(String.valueOf(head.getElement()).compareTo(String.valueOf(element))>0)
                {
                    list.setNext(head);
                    head=list;
                    count++;
                }
                else
                {
                    tail.setNext(list);
                    tail=list;
                    count++;
                }
            }
            else{
                LinearNode<Course> current=null;
                LinearNode<Course>current1=head;
                while(current1!=null&&String.valueOf(current1.getElement()).compareTo(String.valueOf(element))<0)
                {
                    current=current1;
                    current1=current1.getNext();
                }
                if(current==null)
                {
                    list.setNext(head);
                    head=list;
                    count++;
                }
                else {
                    current.setNext(list);
                    list.setNext(current1);
                    count++;
                }
            }
        }
    }
    @Override
    public String toString()
    {
        LinearNode<Course>list=head;
        String result=""+list.getElement();

        while(list.getNext()!=null)
        {
            list=list.getNext();
            result+=list.getElement();
        }
        return result;
    }
}

